package com.cn.jinl.service;

import com.cn.jinl.api.*;
import com.cn.jinl.dao.LineOtherMapper;
import com.cn.jinl.domain.CheckLine;
import com.cn.jinl.domain.LineOther;
import com.cn.jinl.domain.LineOtherExample;
import com.cn.jinl.domain.PlanManage;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@EnableScheduling
@Service
public class LineOtherServiceImpl implements ILineOtherService {

    @Autowired
    private LineOtherMapper lineOtherMapper;

    @Autowired
    private IOnDutyPersonService onDutyPersonService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IPlanManageService planManageService;

    @Autowired
    private ICheckLineService checkLineService;

    @Override
    public Map<String, Object> getLineOtherList(QueryForm queryForm) throws Exception {
        String type = queryForm.getType();
        if (StringUtils.isEmpty(type)) {
            return Maps.newHashMap();
        }
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");

        String banZhang = onDutyPersonService.getOnDutyPersonByDutyAndWhite(dutyDay, whiteDay);
        Map<String, List<String>> deviceMaps = deviceService.getDeviceByProcName("挤塑直切", "");
        List<String> deviceCodes = Lists.newArrayList();
        if (StringUtils.equals(type, "1")) {
            deviceCodes.addAll(deviceMaps.get("U直"));
        } else if (StringUtils.equals(type, "4")) {
            deviceCodes.addAll(deviceMaps.get("普直"));
        } else {
            deviceCodes.addAll(deviceMaps.get("伸直"));
        }
        if (StringUtils.equals(type, "2") || StringUtils.equals(type, "3")) {
            Iterator<String> iterator = deviceCodes.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                String code = next.replace("Z", "");
                int codeMin = 0;
                int codeMax = 0;
                if (StringUtils.equals(type, "2")) {
                    codeMin = 7;
                    codeMax = 12;
                } else if (StringUtils.equals(type, "3")) {
                    codeMin = 13;
                    codeMax = 20;
                }
                if (Integer.parseInt(code) < codeMin || Integer.parseInt(code) > codeMax) {
                    iterator.remove();
                }
            }
        }
        queryForm.setDutyDay(dutyDay);
        queryForm.setWhiteDay(whiteDay);
        List<LineOther> lineOthers = lineOtherMapper.selectLineOutList(queryForm);
        Map<String, LineOther> lineOtherMap = Maps.newHashMap();
        for (LineOther lineOther : lineOthers) {
            lineOtherMap.put(lineOther.getDeviceCode(), lineOther);
        }
        List<LineOther> results = Lists.newArrayList();
        String tableName = "";
        if (StringUtils.equals(type, "1")) {
            tableName = "U直(1-6)";
        } else if (StringUtils.equals(type, "2")) {
            tableName = "伸直(7-12)";
        } else if (StringUtils.equals(type, "3")) {
            tableName = "伸直(13-20)";
        } else if (StringUtils.equals(type, "4")) {
            tableName = "普直(21-26)";
        }
        for (String deviceCode : deviceCodes) {
            if (lineOtherMap.containsKey(deviceCode)) {
                lineOtherMap.get(deviceCode).setTabName(tableName);
                results.add(lineOtherMap.get(deviceCode));
            } else {
                LineOther lineOther = new LineOther();
                lineOther.setDeviceCode(deviceCode);
                lineOther.setTabName(tableName);
                results.add(lineOther);
            }
        }
        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("lineOthers", results);
        resultMap.put("dutyDay", dutyDay);
        resultMap.put("whiteDay", whiteDay);
        resultMap.put("banZhang", banZhang);
        return resultMap;
    }

    @Override
    public Map<String, String> getLineOtherStatusMap(QueryForm queryForm) throws Exception {
        List<LineOther> lineOthers = lineOtherMapper.selectLineOtherStatus(queryForm);
        int openNum = 0;
        Map<String, String> resultMap = Maps.newHashMap();
        for (LineOther lineOther : lineOthers) {
            if (StringUtils.equals("1", lineOther.getLineStatus())) {
                ++openNum;
            }
            resultMap.put(lineOther.getDeviceCode(), lineOther.getGradeValue());
            resultMap.put(lineOther.getDeviceCode() + "#", lineOther.getPlanName());
        }
        resultMap.put("openNum", openNum + "");
        return resultMap;
    }

    @Override
    public String updateLineOther(LineOther lineOther) throws Exception {
        String id = lineOther.getId();
        String lineStatus = lineOther.getLineStatus();
        if (StringUtils.equals(lineStatus, "1")) {
            String taskId = lineOther.getTaskId();
            PlanManage planManage = planManageService.getPlanManageById(taskId);
            if (StringUtils.equals(planManage.getPlanStatus(), "3")) {
                return "该任务已经完成，无法再次生产!";
            }
            String resp = planManageService.updatePlanManageStatus(lineOther.getTaskId(), "2");
            if (StringUtils.isNotEmpty(resp)) {
                return resp;
            }
            CheckLine checkLine = new CheckLine();
            checkLine.setDeviceCode(lineOther.getDeviceCode());
            checkLine.setGradeValue(planManage.getGradeValue());
            checkLine.setPlanName(planManage.getPlanName());
            checkLine.setTabType("1");
            checkLine.setProcType("4");
            checkLine.setValue1(lineOther.getZjValue());
            checkLine.setValue2(lineOther.getCdValue());
            checkLine.setValue3(lineOther.getZlValue());
            checkLine.setValue4(lineOther.getQwValue());
            checkLine.setValue5(lineOther.getZzValue());
            checkLine.setValue6(lineOther.getWgValue());
            checkLine.setValue7(lineOther.getPhValue());
            checkLine.setValue8(lineOther.getJkValue());
            checkLine.setUserName(lineOther.getZjUserName());
            resp = checkLineService.addJSZZQCheckLine(checkLine);
            if (StringUtils.isNotEmpty(resp)) {
                throw new Exception(resp);
            }
            lineOther.setCreateTime(new Date());
        }
        if (StringUtils.isEmpty(id)) {
            return this.addLineOther(lineOther);
        }
        LineOtherExample example = new LineOtherExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<LineOther> lineOthers = lineOtherMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(lineOthers)) {
            return "不存在该记录！";
        }
        int count = lineOtherMapper.updateByExampleSelective(lineOther, example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String addLineOther(LineOther lineOther) throws Exception {
        String id = CommonUtil.generateRandomNum("lineother-");
        lineOther.setId(id);
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");

        lineOther.setWhiteDay(whiteDay);
        lineOther.setDutyDay(dutyDay);
        int count = lineOtherMapper.insertSelective(lineOther);
        return CommonUtil.outStr(count);
    }

    @Override
    public LineOther getOnDutyLineOtherByDeviceCode(String deviceCode) throws Exception {
        if (StringUtils.isEmpty(deviceCode)) {
            return null;
        }
        QueryForm queryForm = new QueryForm();
        queryForm.setDeviceCode(deviceCode);
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        queryForm.setWhiteDay(whiteDay);
        queryForm.setDutyDay(dutyDay);
        List<LineOther> lineOthers = lineOtherMapper.selectLineOutList(queryForm);
        if (CollectionUtils.isEmpty(lineOthers)) {
            return null;
        }
        return lineOthers.get(0);
    }

    @Scheduled(cron = "*/50 * * * * ?")
    public void updateLineOtherStatus() throws Exception {
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        LineOtherExample example = new LineOtherExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andLineStatusEqualTo("1");
        List<LineOther> lineOthers = lineOtherMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(lineOthers)) {
            return;
        }
        long nowData = new Date().getTime();
        for (LineOther lineOther : lineOthers) {
            if (nowData - lineOther.getCreateTime().getTime() >= 3600L * 1000L) {
                LineOtherExample example1 = new LineOtherExample();
                example1.createCriteria().andIdEqualTo(lineOther.getId());
                LineOther update = new LineOther();
                update.setCreateTime(new Date());
                lineOtherMapper.updateByExampleSelective(update,example1);
                this.updateLineOther(lineOther);
            }
        }
//        if(CollectionUtils.isNotEmpty(updateIds)){
//            LineOther update = new LineOther();
//            update.setLineStatus("2");
//            LineOtherExample example1 = new LineOtherExample();
//            example1.createCriteria().andIdIn(updateIds);
//            lineOtherMapper.updateByExampleSelective(update,example1);
//        }
    }
}
